package com.example.tmxk.selvlet;

import com.example.tmxk.bean.NewsBean;
import com.example.tmxk.dao.NewsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class NewsUpdateServlet {
    @WebServlet("/newsEdit.jsp")
    public class NewsAddServlet extends HttpServlet {
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("UTF-8");
            String user = (String) req.getSession().getAttribute("user");
            if (user == null || user.equals("")) {
                resp.sendRedirect("login.jsp");
                return;
            }
            String newsTitle = req.getParameter("upd_News_Title");
            String newsContent = req.getParameter("upd_News_Content");
            int newsId = Integer.parseInt(req.getParameter("newsId"));
            NewsBean newsBean = new NewsBean();//实例化新闻对象
            newsBean.setNewsID(newsId);
            newsBean.setNewsTitle(newsTitle);//设置新闻标题
            newsBean.setNewsContent(newsContent);//设置新闻内容
            newsBean.setAdminName(user);//todo 登录3管理员姓名
            NewsDao newsDao = new NewsDao();//实例化 新闻DAO
            int result = newsDao.update(newsBean);
            if (result >0) {
                resp.sendRedirect("news.jsp");
            }else {
                req.setAttribute("msq", "更新失败");
                req.setAttribute("url", "news.jsp");
                req.getRequestDispatcher("WEB-INF/jsp/newsEdit.jsp").forward(req, resp);
            }
        }
    }
}
